<?php
/**
 * 处理会员到期时间
 * @return
 * @author yuncran@126.com
 */

namespace app\common\task;

use app\model\UserModel;
use think\Exception;
use yunwuxin\cron\Task;

class UserTask extends Task
{

    // 定时任务日志内容
    protected $content = '';
    protected $path = '';
    // protected $daytime = 86400;
    protected $daytime = 5;
    protected $user_id = 1;

    protected $user_field = 'id,pid,nickname,email,level,bio,verification,gender,updatetime,expire_time';

    public function configure()
    {
        //设置每天8点执行
        $this->everyMinute();
        // $this->everyFiveMinutes();
    }

    /**
     * 执行任务
     * @return mixed
     */
    protected function execute()
    {
        try {
            $time = time();
            $userList = UserModel::where('id', '>', $this->user_id)->where('group_id', 1)
                ->whereTime('expire_time', '<', $time)
                ->where('level', '<', 4)                
                ->field($this->user_field)->select();
            foreach ($userList as $key => $val) {
                $this->user_id = $val['id'];
                print 'id == ' . $this->user_id . PHP_EOL;
                $expire_time = strtotime($val['expire_time']);
                if ($time > $expire_time) {
                    UserModel::where('id', $val['id'])->save([
                        'expire_time' => date('Y-m-d H:i:s', $time),
                        'level' => 1,
                        'updatetime' => $time,
                    ]);
                }
            }
            print "****************设置已读成功******************\n";
        } catch (Exception $e) {
            print '设置已读失败:' . $e->getMessage() . "\n";
        }
    }

}